package com.android.clinic.doctors.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.utils.AppManager;
import com.android.clinic.doctors.app.utils.Utils;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 修改密码
 * Created by cheng on 2018/6/13.
 */
public class AmendPasswordActivity extends BaseActivity {

    private EditText etOldPassword, etNewPassword, etConfirmPassword;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_amend_password;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("修改密码");
        etOldPassword = findViewById(R.id.et_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_submit) {
            if (Utils.isEmpty(etOldPassword.getText().toString())) {
                showToast("请输入旧密码!");
                return;
            } else if (Utils.isEmpty(etNewPassword.getText().toString())) {
                showToast("请输入新密码!");
                return;
            } else if (Utils.isEmpty(etNewPassword.getText().toString())) {
                showToast("请输入确认密码!");
                return;
            } else if (etNewPassword.getText().toString().length() < 5) {
                showToast("密码长度过短!");
                return;
            } else if (etNewPassword.getText().toString().length()
                    != etConfirmPassword.getText().toString().length()) {
                showToast("两次密码输入不一致!");
                return;
            }
            updatePassword();
        }
    }

    /**
     * 修改密码
     */
    private void updatePassword() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("doctorId", ClinicDoctorsApplication.getUserInfo().getId());
            jsonObject.put("id", 0);
            jsonObject.put("memberId", 0);
            jsonObject.put("mobile", "");
            jsonObject.put("password", etNewPassword.getText().toString().trim());
            jsonObject.put("status", "");

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());
            HttpClient.getHttpService().updatePassword(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "正在提交..."))
                    .subscribe(new BaseObserver<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showToast("密码修改成功!");
                            ClinicDoctorsApplication.saveUserInfo(null);
                            startActivity(new Intent(AmendPasswordActivity.this, LoginActivity.class));
                            AppManager.getAppManager().finishAllActivity();
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                            finish();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
